<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Contact extends CI_Model {
    public function __construct() {
    	parent::__construct();
    }
	    
    public function getContacts($options = array()) {   
    	$options = _default(array('sortDirection' => 'asc'), $options);
    	$queryFields = array('cnt_id', 'cnt_firstname', 'cnd_middlename',
                             'cnt_lastname', 'cnt_phonenb', 'cnt_email', 
                             'cnt_faxnb', 'cnt_addr1','cnt_addr2', 
                             'cnt_addr3', 'cnt_bank', 'cnt_bankaccount',
                             'cnt_vatid', 'cnt_traderegid', 'cnt_regid');

    	foreach ($queryFields as $field) {
    		if (isset($options[$field]))
    			$this->db->where($field, $options[$field]);
    	}
    	
    	if (isset($options['limit']) && isset($options['offset']))
    		$this->db->limit($options['limit'], $options['offset']);
    	else if (isset($options['limit']))
    		$this->db->limit($options['limit']);
    	
    	if (isset($options['sortBy']))
    		$this->db->order_by($options['sortBy'], $options['sortDirection']);
    	$query = $this->db->get('acs_contacts');
    	if ($query->num_rows() == 0)
    		return FALSE;
    	if (isset($options['cnt_id']))
    		return $query->row_array(0);
    	else
    		return $query->result_array();     
    }
    
    public function countContacts($options = array()) {
    	$queryFields = array('cnt_id', 'cnt_firstname', 'cnd_middlename',
                             'cnt_lastname', 'cnt_phonenb', 'cnt_email', 
                             'cnt_faxnb', 'cnt_addr1','cnt_addr2', 
                             'cnt_addr3', 'cnt_bank', 'cnt_bankaccount',
                             'cnt_vatid', 'cnt_traderegid', 'cnt_regid');
    	foreach ($queryFields as $field) {
    		if (isset($options[$field]))
    			$this->db->where($field, $options[$field]);
    	}
    	$query = $this->db->from('acs_contacts');
    	return $this->db->count_all_results();    
    }
    
    public function updateContact($options = array()) {
		$required = array('cnt_id', 'cnt_firstname', 'cnt_phonenb',
		                  'cnt_email', 'cnt_addr1');
		$fields = array('cnt_firstname', 'cnd_middlename',
                             'cnt_lastname', 'cnt_phonenb', 'cnt_email', 
                             'cnt_faxnb', 'cnt_addr1','cnt_addr2', 
                             'cnt_addr3', 'cnt_bank', 'cnt_bankaccount',
                             'cnt_vatid', 'cnt_traderegid', 'cnt_regid');
        if (!_required($required, $options))
			return FALSE;
         
        foreach ($fields as $field) {
    		if (isset($options[$field]))
    			$this->db->set($field, $options[$field]);
    	}
    	$this->db->where('cnt_id', $options['cnt_id']);
    	$this->db->update('acs_contacts');
        return $this->db->affected_rows();
    }
    
    public function insertContact($options = array()) {
		$required = array('cnt_firstname', 'cnt_phonenb',
		                  'cnt_email', 'cnt_addr1');
		$fields = array('cnt_firstname', 'cnd_middlename',
                             'cnt_lastname', 'cnt_phonenb', 'cnt_email', 
                             'cnt_faxnb', 'cnt_addr1','cnt_addr2', 
                             'cnt_addr3', 'cnt_bank', 'cnt_bankaccount',
                             'cnt_vatid', 'cnt_traderegid', 'cnt_regid');
        if (!_required($required, $options))
			return FALSE;
         
        foreach ($fields as $field) {
    		if (isset($options[$field]))
    			$this->db->set($field, $options[$field]);
    	}
    	$this->db->insert('acs_contacts');
    	return $this->db->insert_id();
    }
    
    public function deleteContact($options = array()) {
    	
    }
}
